package cn.mdmm.service.impl.news;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.mdmm.common.PageObjectUtils;
import cn.mdmm.entity.news.NewsInfoModel;
import cn.mdmm.entity.news.request.NewsInfoRequest;
import cn.mdmm.entity.response.ResponseBodyToGridData;
import cn.mdmm.entity.response.ResponseBodyToOne;
import cn.mdmm.entity.type.TypeInfoModel;
import cn.mdmm.mapper.news.NewsInfoMapper;
import cn.mdmm.service.news.NewsInfoService;
import cn.mdmm.service.type.TypeInfoService;
import cn.modoumama.common.exception.RequiredException;
import cn.modoumama.common.response.Response;
import cn.modoumama.page.GridDataModel;
import cn.modoumama.page.Page;
import cn.modoumama.service.base.impl.BaseServiceImpl;

/**
 * 类名称：NewsInfo对象定义<br>
 * 类描述：新闻业务实现类<br>
 * 类生产方式：工具自动生成代码<br>
 * 创建人：邓强<br>
 * 创建时间：2017-06-02 09:31:54<br>
 * 修改人： <br>
 * 修改时间：<br>
 * 修改备注：<br>
 * 
 * @version V1.0
 */

@Service
public class NewsInfoServiceImpl extends BaseServiceImpl<NewsInfoModel, Long> implements NewsInfoService {
	Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private TypeInfoService typeInfoService;

	@Autowired
	private NewsInfoMapper currMapper;

	@Autowired
	public void setMapper(NewsInfoMapper genericMapper) {
		super.setGenericMapper(genericMapper);
	}

	// ------------------请在此添加自定义方法（开始）------------------
	public GridDataModel getGridDataModelByCondition(Page po) {
		Integer type = (Integer) po.getCondition().get("type");
		List<Integer> types = new ArrayList<Integer>();
		if (type != null) {
			types.add(type);
			TypeInfoModel typeInfo = typeInfoService.getTypeById(type);
			types.addAll(typeInfo.getChildrenIds());
			po.addCondition("types", types);
			po.getCondition().remove("type");
		}
		Integer totalCount = countByCondition(po.getCondition());
		RowBounds rowBounds = new RowBounds(po.getOffset(), po.getPageSize());
		List<NewsInfoModel> results = currMapper.findListByCondition(po.getCondition(), rowBounds);
		return new GridDataModel(results, totalCount.intValue());
	}

	@Override
	public Response getNewsList(NewsInfoRequest request) {
		ResponseBodyToGridData gridData = new ResponseBodyToGridData();
		Integer pageSize = request.getRows();
		pageSize = pageSize == null ? 3 : pageSize;
		request.setRows(pageSize);
		Page po = PageObjectUtils.getPage(request, "PUBLISH_DATE DESC");
		po.addCondition("status", 2);
		po.addCondition("author", request.getAuthor());
		po.addCondition("title", request.getTitle());
		po.addCondition("type", request.getType());
		po.addCondition("level", request.getLevel());
		gridData.setData(findGridDataModelByCondition(po));
		return gridData;
	}

	@Override
	public Response getNewsInfo(NewsInfoRequest request) {
		ResponseBodyToOne bodyToOne = new ResponseBodyToOne();
		if (request.getId() == null) {
			throw new RequiredException("id");
		}
		currMapper.addBrowseNum(request.getId());
		bodyToOne.setData(findById(request.getId()));
		return bodyToOne;
	}

	@Override
	public Integer updateNewsInfo(Long[] ids, NewsInfoModel newsInfo, Long editor) {
		return currMapper.updateNewsInfos(ids, newsInfo, editor);
	}
	// ------------------请在此添加自定义方法（结束）------------------
}
